package org.hive.testserver.domain.model;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.ObjectUtils;

/**
 * Compact history of a test, providing access to total runs, average duration , and so on. This is
 * not meant as a replacement for holding onto a short list of Tests.
 */
public class TestHistory {

	private String name;
	
	private long totalDuration = 0;
	private int totalRuns = 0;
	private Map<TestState,Long> executionsByTestState = new HashMap<TestState, Long>();
	
	public TestHistory(String testName) {
		super();
		this.name = testName;
	}
	
	public String getName() {
		return name;
	}

	public void addFinishedRun(TestState result, long duration) {
		totalDuration += duration;
		totalRuns += 1;
		Long totalResults = timesRun(result);
		executionsByTestState.put(result, totalResults + 1);
	}
	
	public long averageDuration() {
		return totalRuns > 0 ? totalDuration / totalRuns : 0;
	}

	public long timesRun() {
		return totalRuns;
	}

	public long timesRun(TestState testState) {
		return (Long)ObjectUtils.defaultIfNull(executionsByTestState.get(testState), new Long(0));
	}
}
